package com.bl.controller;

import com.bl.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("/getById/{id}")
    // 开启熔断
    @HystrixCommand(fallbackMethod = "getByIdFallback")
    public User getById(@PathVariable("id") Long id){

        String url = "http://user-server/user/getById/" + id;
        // 使用restTemplate发送http请求
        User user = restTemplate.getForObject(url, User.class);

        return user;
    }

    /**
     *  熔断的降级方法 方法名须和接口名一致
     * @param id
     * @return
     */
    public User getByIdFallback(@PathVariable("id") Long id) {
        // 降级方法，返回兜底数据
        return new User(-1L, "触发熔断","服务不可用，请骚后重试");
    }



}
